<?php
  /**
   * @author 2mdc.com
   * @version 
   */

  include_once dirname(__FILE__) . '/cCreateElement.inc';

  class cCreatePage extends cCreateElement {
    static private $rscInstance = NULL;
    protected $xml;

    private function __construct() {
    }

    public function __destruct() {
    }

    public function __toString() {
      return $this->xml;
    }

    static public function getInstance() {
       if(self::$rscInstance == NULL) {
          self::$rscInstance = new cCreatePage();
       }
       return self::$rscInstance;
    }
    
    protected function fGenerateSECTPR($rId = '12240') {    
       $this->xml = '<'.cCreateElement::NAMESPACE.':sectPr '.cCreateDocx::NAMESPACE.':rsidR="' . $rId . '" '.cCreateDocx::NAMESPACE.':rsidRPr="' . $rId . '" '.cCreateDocx::NAMESPACE.':rsidSect="' . $rId . '">__GENERATEHEADERREFERENCE____GENERATEFOOTERREFERENCE____GENERATESECTPR__</'.cCreateElement::NAMESPACE.':sectPr>';
    }
    protected function fGeneratePGSZ($w = '11906', $h = '16838', $orient='') {
    	$xmlAux = '<'.cCreateElement::NAMESPACE.':pgSz '.cCreateElement::NAMESPACE.':w="'.$w.'" '.cCreateElement::NAMESPACE.':h="'.$h.'"';
    	if($orient != '')
    		$xmlAux .= ' '.cCreateElement::NAMESPACE.':orient="'.$orient.'"';
    	$xmlAux .= '></'.cCreateElement::NAMESPACE.':pgSz>__GENERATEPGSZ__';
       	$this->xml = str_replace('__GENERATESECTPR__', $xmlAux, $this->xml);
    }

    protected function fGeneratePGMAR(){
       $top = '1440';
       $right = '1800';
       $bottom = '1440';
       $left = '1800';
       $header = '720';
       $footer = '720';
       $gutter = '0';
       $arrArgs = func_get_args();
       if(isset($arrArgs[0][0]['top']))
       	 $top = $arrArgs[0][0]['top'];
       if(isset($arrArgs[0][0]['bottom']))
       	 $bottom = $arrArgs[0][0]['bottom'];
       if(isset($arrArgs[0][0]['right']))
       	 $right = $arrArgs[0][0]['right'];
       if(isset($arrArgs[0][0]['left']))
       	 $left = $arrArgs[0][0]['left'];
       
       $this->xml = str_replace('__GENERATEPGSZ__', '<'.cCreateElement::NAMESPACE.':pgMar '.cCreateElement::NAMESPACE.':top="'.$top.'" '.cCreateElement::NAMESPACE.':right="'.$right.'" '.cCreateElement::NAMESPACE.':bottom="'.$bottom.'" '.cCreateElement::NAMESPACE.':left="'.$left.'" '.cCreateElement::NAMESPACE.':header="'.$header.'" '.cCreateElement::NAMESPACE.':footer="'.$footer.'" '.cCreateElement::NAMESPACE.':gutter="'.$gutter.'"></'.cCreateElement::NAMESPACE.':pgMar>__GENERATEPGMAR__', $this->xml);
    }

    protected function fGenerateCOLS($num = '', $sep = '', $space = '708', $equalWidth = '') {
       $this->xml = str_replace('__GENERATEPGMAR__', '<'.cCreateElement::NAMESPACE.':cols '.cCreateElement::NAMESPACE.':space="'.$space.'"></'.cCreateElement::NAMESPACE.':cols>__GENERATECOLS__', $this->xml);
    }

    protected function fGenerateCOL($w = '', $space = '708') {
    }

    protected function fGenerateDOCGRID($linepitch = '360') {
       $this->xml = str_replace('__GENERATECOLS__', '<'.cCreateElement::NAMESPACE.':docGrid '.cCreateElement::NAMESPACE.':linePitch="'.$linepitch.'"></'.cCreateElement::NAMESPACE.':docGrid>', $this->xml);
    }
    
    protected function fGenerateBR($type = '') {
    	$this->xml = str_replace('__GENERATER__', '<'.cCreateElement::NAMESPACE.':br '.cCreateElement::NAMESPACE.':type="'.$type.'"></'.cCreateElement::NAMESPACE.':br>', $this->xml);
    }
  
    protected function fGenerateSECTIONSECTPR($rId = '12240') {
    	$this->xml = str_replace('__GENERATEPPR__', '<'.cCreateElement::NAMESPACE.':sectPr '.cCreateDocx::NAMESPACE.':rsidR="' . $rId . '" '.cCreateDocx::NAMESPACE.':rsidRPr="' . $rId . '" '.cCreateDocx::NAMESPACE.':rsidSect="' . $rId . '">__GENERATEHEADERREFERENCE____GENERATEFOOTERREFERENCE____GENERATESECTPR__</'.cCreateElement::NAMESPACE.':sectPr>__GENERATEPPR__', $this->xml);      
    }
    
    protected function fGenerateTITLEPG() {
    	$this->xml = str_replace('__GENERATECOLS__', '<'.cCreateElement::NAMESPACE.':titlePg></'.cCreateElement::NAMESPACE.':titlePg>__GENERATECOLS__', $this->xml);      
    }

    public function fCreatePage($arrDat, $strStyle = '') {
      $this->xml = '';
    }
    
    public function fCreateSECTPR(){
    	$this->xml='';
    	$arrArgs = func_get_args();
    	$this->fGenerateSECTPR();
    	if(!empty($arrArgs[0]['orient']) && $arrArgs[0]['orient'] == 'landscape')
	     	$this->fGeneratePGSZ(16838, 11906, 'landscape');
	    else
	     	$this->fGeneratePGSZ(11906, 16838);
    	$this->fGeneratePGMAR($arrArgs);
    	$this->fGenerateCOLS();
    	if(!empty($arrArgs[0]['titlePage']))
	    	$this->fGenerateTITLEPG();
    	$this->fGenerateDOCGRID();
   }
   
   public function fGeneratePageBreak($strType){
   		$this->xml='';
   		$this->fGenerateP();
   		if($strType != 'line' && $strType != ''){
	   		$this->fGenerateR();   		
	   		$this->fGenerateBR($strType);   		
   		}
   		$this->fCleanTemplate();
   }
  }
?>